//
//  WYCommentController.m
//  Boss
//
//  Created by admin on 2018/8/6.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCommentController.h"
#import "WYCommetNormolCell.h"
#import "WYCommentHeaderCell.h"
#import "WYReplyList.h"
#import "WYCommentList.h"
#import "WYCommentView.h"
@interface WYCommentController ()<UITableViewDelegate,UITableViewDataSource,WYCommentHeaderCellDelegate,WYCommentViewDelegate,WYCommetNormolCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *listArray;
@property (nonatomic,strong)WYCommentList *headModel;
@property (nonatomic,strong)WYCommentView *commentView;
@property (nonatomic,strong)WYReplyList *list;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,assign)NSUInteger page;
@property (nonatomic,assign) BOOL isMe;


@end

@implementation WYCommentController
-(NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setupUI];
    [self loadData];
    [self loadList];
    WEAKSELF
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
        [weakSelf loadList];
        // [weakSelf loadDataWithYear:self.year];
    }];
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification
                                               object:nil];
    
    
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat height = keyboardRect.size.height;
    //self.comment
    [self.commentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-height);
    }];
}
- (void)keyboardWillHide:(NSNotification *)aNotification{
    [self.commentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(164);
    }];
}



-(void)loadData
{
    WEAKSELF
    [WYHttpServer getCommentInfoWithCommentId:self.commentId WithRedpacketType:self.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            
            weakSelf.headModel = [WYCommentList getCommentListOneFronJson:response[@"result"]];
            [weakSelf.tableView reloadData];
          
        }
        
        
        
    }];
}
-(void)loadList
{
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getCommentReplyList:page WithCommentID:self.commentId WithRedPacketType:self.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if (weakSelf.refresh) {
                [weakSelf.listArray removeAllObjects];
            }
            [weakSelf.listArray  addObjectsFromArray:[WYReplyList getReplyListFromJson:response[@"result"][@"detail"]]];
            [weakSelf.tableView reloadData];
            
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
        }
        
        
        
    }];
}


-(void)setupUI
{
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"评论详情";
    [self.tableView registerNib:[UINib nibWithNibName:@"WYCommetNormolCell" bundle:nil] forCellReuseIdentifier:@"WYCommetNormolCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WYCommentHeaderCell" bundle:nil] forCellReuseIdentifier:@"WYCommentHeaderCell"];
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    self.tableView.estimatedRowHeight = 100;
    
    self.commentView= [[WYCommentView alloc]init];
    self.commentView.delegate = self;
    self.commentView.comment.text = @"回复";
    [self.view addSubview:self.commentView];
    
    [self.commentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(164);
        make.bottom.mas_equalTo(164);
    }];
    
    
}

-(void)clickSendButton
{
    WEAKSELF
    if (!self.commentView.textView.text.length) {
        [self toast:@"请输入评论内容"];return;
    }
    
    [WYHttpServer addRedPacketCommentReplayWithMessageId:self.headModel.messageId WithCommentId:self.headModel.commentId WithToReplayId:self.list.replyId WithContent:self.commentView.textView.text WithToUserId:self.list.fromUserId WithToUserName:self.list.fromUserName WithRedPacketType:self.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            if (success) {
                weakSelf.refresh = YES;
                [weakSelf loadList];
            }else{
                [weakSelf toast:errorMessage];
            }
        }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    if (self.headModel) {
        return self.listArray.count;
    }else{
        return 0;
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section!=0) {
        WYCommetNormolCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYCommetNormolCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"WYCommetNormolCell" owner:nil options:nil].firstObject;
        }
         WYReplyList *list = self.listArray[indexPath.row];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.headModel = self.headModel;
        [cell setList:list];
        cell.delegate = self;
        return cell;
    }else{
        WYCommentHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYCommentHeaderCell" forIndexPath:indexPath];
        if (self.headModel) {
            [cell setList:self.headModel];
        }
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==0) {
        UIView *view = [[UIView alloc]init];
        UIView *lineView = [[UIView alloc]init];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.mas_equalTo(10);
        }];
        lineView.backgroundColor = [UIColor hexStringToColor:@"#f7f7f7" alpha:1.0];
        UILabel *label = [[UILabel alloc]init];
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.top.equalTo(lineView.mas_bottom).offset(10);
        }];
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"全部回复";
        label.textColor = [UIColor hexStringToColor:@"#999999" alpha:1.0];
        return view;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        [self.commentView.textView becomeFirstResponder];
        WYReplyList *list = [[WYReplyList alloc]init];
        list.replyId = @"0";
        list.fromUserName = self.headModel.userName;
        list.fromUserId  = self.headModel.userId;
        self.list = list;
        self.commentView.textView.placeholder = [NSString stringWithFormat:@"回复 %@",self.headModel.userName];
        
    }
    if (indexPath.section==1) {
        WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
        WYReplyList *list = self.listArray[indexPath.row];
        self.list = list;
        if ([list.fromUserId isEqualToString:userMessage.id]) {
            return;
        }
        self.commentView.textView.placeholder = [NSString stringWithFormat:@"回复 %@",list.fromUserName];
        [self.commentView.textView becomeFirstResponder];
    }
}
-(void)didDeleteButtonWithReplyId:(NSString *)replyId
{
    WEAKSELF
    [WYHttpServer deleteCommnetReplyWithReplyId:replyId WithRedpacketType:self.redpacketType SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            weakSelf.refresh = YES;
            [weakSelf loadList];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
    }];
    
}

-(void)didSelectButton
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==0) {
        return 50;
    }
    return 0.0000001;
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
    }
    return _tableView;
}
@end
